<template>
  <el-card>
    <el-button type="primary" icon="Plus" @click="addBannerList"
      >添加轮播图</el-button
    >
  </el-card>
  <el-card class="content">
    <el-table border :data="bannerList" max-height="570">
      <el-table-column
        label="序号"
        type="index"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column label="轮播图" align="center" width="400">
        <template v-slot="{ row }">
          <el-image :src="row.bannerUrl"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" align="center" width="120">
        <template #default="{ row }">
          <el-tag type="success" effect="dark" v-if="row.isShow == 1"
            >展示中</el-tag
          >
          <el-tag type="danger" effect="dark" v-else> 下架中 </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template #default="{ row }">
          <span>{{ row.createAt.split("T")[0] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center">
        <template #default="{ row }">
          <span>{{ row.updateAt.split("T")[0] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button
            type="success"
            size="small"
            icon="Top"
            title="上架"
            v-if="row.isShow !== 1"
            @click="updateBanner(row)"
          ></el-button>
          <el-button
            type="info"
            size="small"
            icon="Bottom"
            title="下架"
            @click="updateBanner(row)"
            v-else
          ></el-button>

          <el-popconfirm title="你确定要删除该图吗?" @confirm="deleteBanner(row)" >
            <template #reference>
              <el-button
                type="danger"
                size="small"
                icon="Delete"
                title="删除"
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="page"
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      background
      layout="jumper,prev, pager, next,total, sizes,"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
  <el-dialog
    width="600"
    v-model="centerDialogVisible"
    title="添加轮播图"
    center
    @open="openDialog"
  >
    <el-form :model="bannerInfo" label-width="100px" :rules="rules" ref="form">
      <el-form-item label="轮播图:" prop="bannerUrl">
        <el-upload
          class="avatar-uploader"
          action="http://121.40.96.76:3000/api/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img
            v-if="bannerInfo.bannerUrl"
            :src="bannerInfo.bannerUrl"
            class="avatar"
          />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="状态:" prop="isShow">
        <el-select
          v-model="bannerInfo.isShow"
          placeholder="请选择你的状态"
          style="width: 60%"
        >
          <el-option label="展示中" :value="1"></el-option>
          <el-option label="下架中" :value="2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confromAdd"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { getBannerListAPI, addBannerAPI, updateBannerAPI , deleteBannerAPI } from "@/api/banner";
import { ElMessage } from "element-plus";
let bannerList = ref([]);
let centerDialogVisible = ref(false);
let rules = reactive({
  bannerUrl: [{ required: true, message: "请上传图片", trigger: "blur" }],
  isShow: [{ required: true, message: "请选择状态", trigger: "blur" }],
});
let pageNo = ref(1);
let pageSize = ref(5);
let total = ref(0);
let bannerInfo = reactive({
  bannerUrl: "",
  isShow: "",
});
let form = ref(null);
onMounted(() => {
  getBannerList();
});
//获取轮播图列表
const getBannerList = async () => {
  let result = await getBannerListAPI({
    pageNo: pageNo.value,
    pageSize: pageSize.value,
  });
  bannerList.value = result.data;
  total.value = result.total;
};
//上传图片
const handleAvatarSuccess = (res) => {
  bannerInfo.bannerUrl = res.data;
};
const openDialog = () => {
  bannerInfo.bannerUrl = "";
  bannerInfo.isShow = "";
  form.value.resetFields();
};
//上传之前的回调
const beforeAvatarUpload = (file) => {
  //只能是jpg或者png大小为4m
  const isJPG = file.type === "image/jpeg";
  const isPNG = file.type === "image/png";
  const isLt4M = file.size / 1024 / 1024 < 4;
  if (!isJPG && !isPNG) {
    ElMessage.error("上传头像图片只能是 JPG/PNG 格式!");
  }
  if (!isLt4M) {
    ElMessage.error("上传头像图片大小不能超过 4MB!");
  }
  return (isJPG || isPNG) && isLt4M;
};
//分页当前页发生改变
const handleCurrentChange = (val) => {
  pageNo.value = val;
  getBannerList();
};
//分页条数发生改变
const handleSizeChange = (val) => {
  pageSize.value = val;
  getBannerList();
};
//添加轮播图
const addBannerList = () => {
  centerDialogVisible.value = true;
};
const confromAdd = async () => {
  await form.value.validate();
  await addBannerAPI(bannerInfo);
  ElMessage.success("添加成功");
  getBannerList();
  centerDialogVisible.value = false;
};
//修改轮播图
const updateBanner = async (row) => {
  bannerInfo.isShow = row.isShow == 1 ? 2 : 1;
  await updateBannerAPI(row._id, { isShow: bannerInfo.isShow });
  ElMessage({
    type: row.isShow == 1 ? "info" : "success",
    message: row.isShow == 1 ? "下架成功" : "上架成功",
  });
  getBannerList();
};
//删除轮播图
const deleteBanner = async (row)=>{
  await deleteBannerAPI(row._id)
  ElMessage.success('删除成功')
  getBannerList()
}
</script>

<style lang="scss" scoped>
.content {
  margin-top: 10px;
  .page {
    margin-top: 10px;
    display: flex;
    justify-content: center;
  }
}
</style>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
